package enduringLink;

import com.caucho.hessian.io.Hessian2Input;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.io.ByteArrayInputStream;
import java.util.List;

public class RequestDecode extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
        while(buf.readableBytes() >= 4) {
            int length = buf.getInt(buf.readerIndex());//从哪里读取，读多少，但是readindex不变

            if(buf.readableBytes() >= 4 + length){
                //处理指针
                buf.readBytes(4);  //移动指针到数据开始的位置
                byte[] data = new byte[length];
                buf.readBytes(data);
                ByteArrayInputStream in = new ByteArrayInputStream(data);
                Hessian2Input input = new Hessian2Input(in);
                MyContent myContent = (MyContent) input.readObject();
                in.close();
                input.close( );

                out.add(myContent);


            }else{
                break;
            }


        }
    }
}
